package com.wlw.admin.openeye.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.MyApplication
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.mvp.model.bean.CategoryBean
import com.wlw.admin.openeye.ui.activity.CategoryDetailActivity

class CategoryAdapter(mContext: Context, categoryList: ArrayList<CategoryBean>) : CommonAdapter<CategoryBean>(mContext, categoryList, R.layout.item_category) {

    private var textTypeface: Typeface? = null

    init {
        textTypeface = Typeface.createFromAsset(MyApplication.context.assets, "fonts/FZLanTingHeiS-DB1-GB-Regular.TTF")
    }

    fun setData(categoryList: ArrayList<CategoryBean>) {
        mData.clear()
        mData = categoryList
        notifyDataSetChanged()
    }

    override fun bindData(holder: ViewHolder, data: CategoryBean, position: Int) {
        holder.setText(R.id.tv_category_name, data.name)
        holder.getView<TextView>(R.id.tv_category_name).typeface = textTypeface
        holder.setImagePath(R.id.iv_category, object : ViewHolder.HolderImageLoader(data.bgPicture) {
            override fun loadImage(iv: ImageView, path: String) {
                Glide.with(mContext).load(path).transition(DrawableTransitionOptions().crossFade())
                        .thumbnail(0.5f)
                        .into(iv)
            }
        })
        holder.itemView.setOnClickListener {
            val intent = Intent(mContext as Activity, CategoryDetailActivity::class.java)
            intent.putExtra(Constants.BUNDLE_CATEGORY_DATA, data)
            mContext.startActivity(intent)
        }
    }
}